//
//  CartViewController.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/7/5.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit

class CartViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var topBgImageView = UIImageView()
    var titleLabel = UILabel()
    var midView = UIView()
    var tableView = UITableView()
    var noMoreView = UIView()
    var dataSource = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configTopView()
        configTableView()
        
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden=true
        getDataSource()
    }
    func getDataSource()  {
        CartManager.sharedManager().getAllGoodsInCart { (result, allGoods) in
            dataSource = NSMutableArray(array: allGoods)
            tableView.reloadData()
        }
    }
    func configTopView() {
        self.topBgImageView = UIImageView(image: UIImage(named: "Mine_Top_bgView"))
        self.topBgImageView.frame = CGRect(x: 0, y: 0, width: DeviceWidth, height: Height_StatusBar+108)
        self.topBgImageView.isUserInteractionEnabled = true
        self.view.addSubview(self.topBgImageView)
        
        self.midView = UIView(frame: CGRect(x: 0, y: self.topBgImageView.maxY-20, width: DeviceWidth, height: 20))
        
        let cornerRadiusPath = UIBezierPath(roundedRect: self.midView.bounds, byRoundingCorners: [UIRectCorner.topRight, UIRectCorner.topLeft], cornerRadii: CGSize(width: 8, height: 8))
        let cornerRadiusLayer = CAShapeLayer()
        cornerRadiusLayer.frame = self.midView.bounds
        cornerRadiusLayer.path = cornerRadiusPath.cgPath
        self.midView.layer.mask = cornerRadiusLayer
        self.view.addSubview(self.midView)
        
        self.titleLabel = UILabel.createLabel(frame: CGRect(x: leftDistance, y: Height_StatusBar+25, width: DeviceWidth/2, height: 16), title: "购物车", textColor: ColorWithHex("#ffffff"), isCenter: false, font: 17, board: false)
        self.view.addSubview(self.titleLabel)
    }
    func configTableView() {
        tableView =  UITableView.createTabelView(CartTableViewCell.classForCoder(), "cell_token", self)
        let tableY = midView.maxY
        tableView.frame = CGRect(x: 0, y: tableY, width: DeviceWidth+1, height: DeviceHeight-tableY-Height_TabBar)
        tableView.tintColor = kMainColor
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 96
    }
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return 36
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = viewForHeader(section: section)
        return headerView
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let shopItem:CartListItem  = dataSource[section] as! CartListItem
        let goodsInShop = shopItem.allGoods
        return goodsInShop.count
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CartTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell_token", for: indexPath) as! CartTableViewCell
        let shopItem:CartListItem  = dataSource[indexPath.section] as! CartListItem
        let goodsInShop = shopItem.allGoods
        let item = goodsInShop[indexPath.row]
        cell.setCartGoodsItem(cartGoodsItem: item)
        return cell
    }
    private func viewForHeader(section:NSInteger) ->UIView {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: DeviceWidth, height: 36))
        headerView.backgroundColor = UIColor.white
        headerView.tag = section+10
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.gotoShopMain(_ :)))
        headerView.addGestureRecognizer(tap)
        
        let shopNameLabel = UILabel.createLabel(frame: CGRect(x: 38, y: 12, width: 100, height: 14), title: "店铺", textColor: ColorWithHex("#333333"), isCenter: false, font: 14, board: true)
        headerView.addSubview(shopNameLabel)
        
        let accImage = UIImageView(frame: CGRect(x: shopNameLabel.maxX+24, y: shopNameLabel.minY+2, width: 6, height: 10))
        accImage.image = UIImage(named: "my_right_acc")
        
        headerView.addSubview(accImage)
        return headerView
    }
    @objc func gotoShopMain(_ tap:UITapGestureRecognizer) {
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
